﻿@{
    ViewBag.Title = "Index";
}



<!-- Main content -->
<section class="content" ng-app="bank" ng-controller="bankController">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header"> 
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">银行账户</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav"></ul>
                <form class="navbar-form navbar-left" role="search">
                    <div class="form-group">
                        <button class="btn btn-default" data-toggle="modal" data-target="#addBankCardModal" ng-click="addBankCard()">+新增账户</button>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search" ng-model="searchCardNo">
                    </div>
                    <button ng-click="loadBankCardList()" class="btn btn-default">查询</button>
                </form>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container-fluid -->
    </nav>

    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <!-- /.box-header -->
                <div class="box-body">
                    <table class="table table-hover">
                        <tr>
                            <th style="width: 10px">#</th>
                            <th>卡号</th>
                            <th>余额</th>
                            <th>银行</th>
                            <th></th>
                        </tr>
                        <tr ng-repeat="(index,card) in cardList">
                            <td>{{pageSize*(pageIndex-1)+index+1}}.</td>
                            <td>{{card.cardNo|bankCardNumberFormat:4}}</td>
                            <td>
                                <span class="badge bg-red" ng-show="card.amount>0">{{card.amount|currency:"￥"}}</span>
                                <span class="badge bg-green" ng-show="card.amount<=0">{{card.amount|currency:"￥"}}</span>
                            </td>
                            <td>{{card.bankName}}</td>
                            <td>
                                <button class="btn btn-default btn-xs" ng-click="editBankCard(card.id)" data-toggle="modal" data-target="#addBankCardModal">编辑</button>
                                <button class="btn btn-default btn-xs" ng-click="deleteBankCard(card.id)">删除</button>
                            </td>
                        </tr>
                    </table>
                </div>
                <!-- /.box-body -->
                <div class="box-footer clearfix">
                    <span>{{$scope.total}}</span>
                    <ul class="pagination pagination-sm no-margin pull-right">
                        <li><a href="#">&laquo;</a></li>
                        <li ng-repeat="i in pageRange " class="{{i==pageIndex?'active':''}}"><a href="#">{{i}}</a></li>
                        <li><a href="#">&raquo;</a></li>
                    </ul>
                </div>

                <div class="overlay" ng-show="bankCardLoading">
                    <i class="fa fa-refresh fa-spin"></i>
                </div>
            </div>
            <!-- /.box -->
        </div>
    </div>


    <div class="modal" id="addBankCardModal" role="dialog" aria-labelledby="addBankCardModalTitle">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="addBankCardModalTitle">编辑</h4>
                </div>
                <div class="modal-body">
                    <form name="editForm" class="form-horizontal" ng-submit="editForm.$valid&&saveBankCard()" novalidate="novalidate">
                        <div class="box-body">
                            <div class="form-group {{editForm.cardNo.$dirty?(editForm.cardNo.$invalid?'has-error':'has-success'):''}}">
                                <label for="inputEmail3" class="col-sm-2 control-label">卡号</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control"   name="cardNo" placeholder="卡号" ng-model="edit.cardNo" required ng-pattern="/^\d{10,}$/">
                                </div>
                            </div>
                            <div class="form-group {{editForm.amount.$dirty?(editForm.amount.$invalid?'has-error':'has-success'):''}}">
                                <label for="amount" class="col-sm-2 control-label">余额</label>
                                <div class="col-sm-10">
                                    <input type="text"   name="amount" class="form-control" placeholder="余额" ng-model="edit.amount" value="0" required ng-pattern="/^\d+(\.\d+)?$/"/>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
<div class="box-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关 闭</button>
                    <button type="submit" class="btn btn-primary pull-right"  ng-disabled="editForm.$invalid||saving">保 存</button>
                </div>
                    </form>
                </div>
                
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->

</section>
<!-- /.content -->




@section scripts{

    <script src="//cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
    <script type="text/javascript">
        angular.module("bank", [])
            .factory("actionUrl", function() {
                return {
                    saveBankCard: "@Url.Action("SaveBankCard")",
                    loadBankCardList: "@Url.Action("LoadBankCardList")",
                    deleteBankCard: "@Url.Action("DeleteBankCard")"
                };
            })
            .filter("bankCardNumberFormat", function() {

                return function(input, len) {
                    len = len || 4;
                    if (input) {
                        var ary = [];
                        for (var i = 0; i < input.length; i += len) {
                            ary.push(input.substr(i, len));
                        }
                        return ary.join(" ");
                    }
                    return input;
                };
            })
            .controller("bankController", function($scope, $http, actionUrl) {
                $scope.edit = {
                    cardNo: "",
                    amount: 0.00,
                    id: 0,
                    reset: function() {
                        this.cardNo = "";
                        this.amount = 0.00;
                        this.id = 0;
                    }
                };

                $scope.addBankCard = function() {
                    $scope.edit.reset();
                };
                $scope.editBankCard = function(id) {
                    $scope.edit.reset();
                    var cardData = _.find($scope.cardList, function(c) { return c.id == id; });
                    if (cardData) {
                        $scope.edit.id = cardData.id;
                        $scope.edit.amount = cardData.amount;
                        $scope.edit.cardNo = cardData.cardNo;
                    }
                };
                $scope.deleteBankCard = function(id) {
                    if (confirm("删除后无法恢复，确定要删除吗？")) {
                        $http.post(actionUrl.deleteBankCard, { id: id })
                            .success(function (r) {
                                if (r.success) {
                                    $scope.loadBankCardList();
                                }
                            })
                            .error(function() {
                            });
                    }
                };
                $scope.saveBankCard = function() {
                    $scope.saving = true;
                    $http.post(actionUrl.saveBankCard, $scope.edit).success(function(r) {
                        if (r.success) {
                            $scope.edit.reset();
                            $scope.loadBankCardList();
                            $("#addBankCardModal").modal('hide');
                        }
                        $scope.saving = false;
                    }).error(function() {
                        $scope.saving = false;
                    });

                };
                $scope.pageIndex = 1;
                $scope.pageSize = 20;
                $scope.showPageCount = 5; //要显示的页数
                $scope.pageRange = [];

                function setPageRange() {
                    var totalPageCount = ($scope.total + $scope.pageSize - 1) / $scope.pageSize; // 总页数
                    var start = $scope.pageIndex;
                    ;
                    // 判断当前页码位置 前(页码小于显示页数) 后（页码大于总页数-显示页数）
                    if ($scope.pageIndex <= $scope.showPageCount) {
                        start = 1;
                    } else if ($scope.pageIndex >= totalPageCount - $scope.showPageCount) {
                        start = totalPageCount - $scope.showPageCount;
                    }
                    var ary = [];
                    for (var i = start; i <= $scope.showPageCount && i <= totalPageCount; i++) {
                        ary.push(i);
                    }
                    return ary;
                }

                $scope.bankCardLoading = true;
                $scope.cardList = []; // [{ id: 1, cardNo: "6212260200024310340", amount: 156890, bankName: "工商银行" }, { id: 2, cardNo: "6226620204976385", amount: -156890, bankName: "光大银行" }];//{cardNo:"6212260200024310340",amount:0}
                $scope.loadBankCardList = function() {
                    $scope.bankCardLoading = true;
                    $http.post(actionUrl.loadBankCardList, {cardNo:$scope.searchCardNo, pageIndex: $scope.pageIndex, pageSize: $scope.pageSize })
                        .success(function(r) {
                            $scope.cardList = r.data;
                            $scope.total = r.total;
                            $scope.pageRange = setPageRange();
                            $scope.bankCardLoading = false;
                        })
                        .error(function() {
                            $scope.bankCardLoading = false;
                        });
                };

                $scope.loadBankCardList();
                //$("[data-mask]").inputmask();
            });
    </script>
}